<?php
/**
 * armory好友推荐
 * 
 * @author 	  jiaowei<jiaowei@178.com>
 * @version   $Id$
 */
class friends_controller_armory extends Controller
{	
	public function init()
	{
		$this->headerView = $this->viewDir."/_header.phtml";
		$this->requireLogin();
	}
	
	/**
	 * 列表
	 *
	 * @display true
	 */
	public function index_action()
	{
		$uid  = self::$login['uid'];
		$page = _('page', 1);
		
		$friend = new armory_util_friendu();
		$res = $friend->getCommendFriend($uid, $page, 10);
		
		$this->list = null;
		if ($res['user'] && is_array($res['user'])) {
			$this->list  = $this->_formatUserInfo($res['user']);
			$this->total = $res['total']['COUNT'];
			$this->role  = $res['role'];
			$this->pageInfo = $this->genPages($res['total']);
		}
	}
	
	/**
	 * 分页获取会员列表
	 *
	 * @display false
	 */
	public function embed_action()
	{
		$uid  = self::$login['uid'];
		$page = _('page', 1);
		
		$friend = new armory_util_friendu();
		$res = $friend->getCommendFriend($uid, $page, 4, true);
		
		$this->list = null;
		if ($res['user'] && is_array($res['user'])) {
			$this->list  = $this->_formatUserInfo($res['user']);
			$this->role  = $res['role'];
			$this->prepage  = 0;
			$this->nextpage = 0;
			if ($page > 1) $this->prepage = $page - 1;
			if ($page < $res['total']['PAGECOUNT']) $this->nextpage = $page + 1;
			ob_start();
			include_once($this->viewDir . '/embed.phtml');
			$data = ob_get_contents();
			
			ob_end_clean();
			exit($data);
		}
		exit('');		
	}
	
	private function _formatUserInfo($users)
	{
		$uids = array();
		$armory = array();
		foreach ($users as $v) {
			$uids[] = $v['uid'];
			$armory[$v['uid']]['cname']    = $v['cname'];
			$armory[$v['uid']]['class']    = $v['class'];
			$armory[$v['uid']]['relationId'] = $v['relationId'];
		}
		$result = friends_util_cache::getUserInfo($uids);
		unset($uids);
		foreach ($result as $k => $v) {
			$result[$k]['cname'] 	= $armory[$v['id']]['cname'];
			$result[$k]['class'] 	= $armory[$v['id']]['class'];
			$result[$k]['relationId'] = $armory[$v['id']]['relationId'];
		}
		
		unset($armory);
		return $result;
	}
}
